package org.dong.Jdbc;

import sun.misc.Request;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Parameter;
import java.sql.*;
import java.util.Properties;

public class JdbcUtil {

    private static  String url;
    private static  String username;
    private static  String password;

    static{
        try {
//            Class.forName("com.mysql.jdbc.Driver");

            //读取配置文件信息
            Properties properties = new Properties();
            properties.load(new FileInputStream("JdbcMysql.properties"));
            url = properties.getProperty("url");
            username = properties.getProperty("username");
            password = properties.getProperty("password");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection(){
        try {
            return DriverManager.getConnection(url,username,password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void closeDb(ResultSet rs, Connection conn, PreparedStatement preparedStatement){
        try {
            if(rs != null)
                rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        try {
            if(conn != null)
               conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        try {
            if(preparedStatement != null)
                preparedStatement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }}
